file(GLOB _all_libdirs libs/*)
foreach(_dir ${_all_libdirs})
  if(EXISTS ${_dir}/src)
    file(GLOB_RECURSE _current_sources ${_dir}/src/*.cpp)
    list(APPEND _sources ${_current_sources})
  endif()
endforeach()

if(WIN32)
  set(_exclude_files "/posix/|/pthread/|/icu/")
else()
  set(_exclude_files "/windows/|/win32/")
endif()

set(_exclude_files "${_exclude_files}|serialization/src/shared_ptr_helper.cpp")

foreach(_file ${_sources})
  if(${_file} MATCHES ${_exclude_files})
    set_source_files_properties(${_file} PROPERTIES HEADER_FILE_ONLY TRUE)
  endif()
endforeach()

file(GLOB_RECURSE _headers boost/*.hpp boost/*.ipp boost/*.h boost/*.inc)

if(UNIX)
  add_definitions(-DBOOST_LOCALE_WITH_ICONV -DBOOST_LOG_WITHOUT_DEBUG_OUTPUT)
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
  set(_ssse3_compile_flags "-march=core2 -msse -msse2 -msse3 -mssse3")
  if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.7
     OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 4.7)
    set(_avx2_compile_flags "-march=core-avx2 -mavx -mavx2 -fabi-version=0")
  endif()
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  set(_ssse3_compile_flags "-march=core2 -msse -msse2 -msse3 -mssse3")
  set(_avx2_compile_flags "-march=core-avx2 -mavx -mavx2")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
  if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
    set(_ssse3_compile_flags "-march=core2")
    set(_avx2_compile_flags "-march=core-avx2 -fabi-version=0")
  elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
    set(_ssse3_compile_flags "-march=core2")
    set(_avx2_compile_flags "-march=core-avx2 -fabi-version=0")
  elseif(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
    set(_ssse3_compile_flags "/QxSSSE3")
    set(_avx2_compile_flags "/arch:CORE-AVX2")
  else()
    message(FATAL "Unknown platform")
  endif()
elseif(MSVC)
  if(MSVC_VERSION GREATER 1800 OR MSVC_VERSION EQUAL 1800)
    set(_avx2_compile_flags "/arch:AVX2")
  endif()
else()
  message(FATAL "Unknown compiler")
endif()

foreach(_file ${_sources})
  if(${_file} MATCHES ".*avx2\\.cpp")
    if(NOT _avx2_compile_flags)
      set_source_files_properties(${_file} PROPERTIES
        HEADER_FILE_ONLY TRUE)
    else()
      set_source_files_properties(${_file} PROPERTIES
        COMPILE_FLAGS ${_avx2_compile_flags})
    endif()
  elseif(${_file} MATCHES ".*ssse3\\.cpp")
    if(_ssse3_compile_flags)
      set_source_files_properties(${_file} PROPERTIES
        COMPILE_FLAGS ${_ssse3_compile_flags})
    endif()
  elseif(${_file} MATCHES ".*event_log_backend\\.cpp")
    set_source_files_properties(${_file} PROPERTIES
      HEADER_FILE_ONLY TRUE)
  endif()
endforeach()

include_directories(. boost)
add_library(boost_static ${_sources} ${_headers})
